You write custom CUDA kernels to replace the pytorch operators in the given GeGLU architecture to get speedups.

You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining chunk+gelu+elementwise_mul), or algorithmic changes (such as optimized memory access patterns). You are only limited by your imagination.
PyTorch C++/CUDA Extension: Inline compilation via torch.utils.cpp_extension.load_inline.

Element‑wise MSE Kernel: Computes squared difference (pred_actions[i] - expert_actions[i])^2 per element.

Fixed Block Size: 256 threads per block, grid size determined by total number of elements.

Built‑in Mean Reduction: Returns the mean of all squared differences directly in the CUDA wrapper.

Minimal Python Interface: Forward pass calls the compiled CUDA function imitation_cuda.

Verbose Compilation: Displays compilation details (verbose=True).




Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is:
import torch
import torch.nn as nn


class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

    def forward(self, pred_actions: torch.Tensor, expert_actions: torch.Tensor) -> torch.Tensor:
        loss = ((pred_actions - expert_actions) ** 2).mean()
        return loss


batch_size = 32
action_dim = 4


def get_inputs():
    pred_actions = torch.randn(batch_size, action_dim)
    expert_actions = torch.randn(batch_size, action_dim)
    return [pred_actions, expert_actions]


def get_init_inputs():
    return []